//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SimplePolynomialFitting.h"
using namespace Cephei::QL::Termstructures::Yield;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (UInt32 degree, Microsoft::FSharp::Core::FSharpOption<Boolean>^ constrainAtZero) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phSimplePolynomialFitting = NULL;
#endif
        QuantLib::Natural _degree = (QuantLib::Natural)ValueHelper::Convert (degree); //d
        bool _constrainAtZero = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (constrainAtZero) ? (bool)ValueHelper::Convert (constrainAtZero->Value) : true); //4
        _ppSimplePolynomialFitting = new boost::shared_ptr<QuantLib::SimplePolynomialFitting> (new QuantLib::SimplePolynomialFitting ( _degree,  _constrainAtZero ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (boost::shared_ptr<QuantLib::SimplePolynomialFitting>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSimplePolynomialFitting = NULL;
#endif
	_ppSimplePolynomialFitting = &childNative;
    
}
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (QuantLib::SimplePolynomialFitting& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phSimplePolynomialFitting = NULL;
#endif
	_ppSimplePolynomialFitting = new boost::shared_ptr<QuantLib::SimplePolynomialFitting> (&childNative);
    
    _SimplePolynomialFittingOwner = owner;
    
}

Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (CSimplePolynomialFitting^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSimplePolynomialFitting = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSimplePolynomialFitting = new boost::shared_ptr<QuantLib::SimplePolynomialFitting> (copy->GetShared());
        
    }
}
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSimplePolynomialFitting = NULL;
#endif
	if (!t->IsSubclassOf(CSimplePolynomialFitting::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (QuantLib::Handle<QuantLib::SimplePolynomialFitting>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSimplePolynomialFitting = &childNative;
	_ppSimplePolynomialFitting = &static_cast<boost::shared_ptr<QuantLib::SimplePolynomialFitting>>(childNative.currentLink());
    
    _SimplePolynomialFittingOwner = owner;
}
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (QuantLib::Handle<QuantLib::SimplePolynomialFitting> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phSimplePolynomialFitting = &childNative;
	_ppSimplePolynomialFitting = &static_cast<boost::shared_ptr<QuantLib::SimplePolynomialFitting>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::CSimplePolynomialFitting (QuantLib::SimplePolynomialFitting childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phSimplePolynomialFitting = NULL;
#endif
	_ppSimplePolynomialFitting = new boost::shared_ptr<QuantLib::SimplePolynomialFitting> (new QuantLib::SimplePolynomialFitting (childNative));
    
}
#endif

Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::~CSimplePolynomialFitting ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppSimplePolynomialFitting != NULL)
    {
	    delete _ppSimplePolynomialFitting;
        _ppSimplePolynomialFitting = NULL;
    }
}
Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::!CSimplePolynomialFitting ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppSimplePolynomialFitting != NULL)
    {
	    delete _ppSimplePolynomialFitting;
    }
}
QuantLib::SimplePolynomialFitting& Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::GetReference ()
{
    if (_ppSimplePolynomialFitting == NULL) throw REFNEW NativeNullException ();
	return **_ppSimplePolynomialFitting;
}
boost::shared_ptr<QuantLib::SimplePolynomialFitting>& Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::GetShared ()
{
    if (_ppSimplePolynomialFitting == NULL) throw REFNEW NativeNullException ();
	return *_ppSimplePolynomialFitting;
}
QuantLib::SimplePolynomialFitting* Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::GetPointer ()
{
    if (_ppSimplePolynomialFitting == NULL) throw REFNEW NativeNullException ();
	return &**_ppSimplePolynomialFitting;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SimplePolynomialFitting>& Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::GetHandle ()
{
	if (_phSimplePolynomialFitting == NULL)
	{
		_phSimplePolynomialFitting = new Handle<QuantLib::SimplePolynomialFitting> (*_ppSimplePolynomialFitting);
	}
	return *_phSimplePolynomialFitting;
}
#endif
bool Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting::HasNative () 
{
	return (_ppSimplePolynomialFitting != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Yield::ISimplePolynomialFitting^ Cephei::QL::Termstructures::Yield::CSimplePolynomialFitting_Factory::Create (UInt32 degree, Microsoft::FSharp::Core::FSharpOption<Boolean>^ constrainAtZero)
{
    return REFNEW CSimplePolynomialFitting ( degree,  constrainAtZero);
}
